package com.ist.viewpagerhelper.view;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

import com.ist.viewpagerhelper.bean.ViewPagerBean;
import com.ist.viewpagerhelper.indicator.NormalIndicator;
import com.ist.viewpagerhelper.indicator.TransIndicator;
import com.ist.viewpagerhelper.indicator.ZoomIndicator;

import java.util.ArrayList;
import java.util.List;

public class GlideViewPager extends ViewPager {

    private Context mContext;
    private List<View> mViews;

    public GlideViewPager(Context context) {
        this(context,null);
    }
    public GlideViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;

    }

    public void setViewPagerBean(ViewPagerBean bean){
        if (bean != null){
            mViews = new ArrayList<>();
            if (bean.views == null){
                return;
            }
            mViews.addAll(bean.views);
            setAdapter(new CusTomAdapter());
            if (bean.bottomLayout != null) {
                if (bean.bottomLayout instanceof ZoomIndicator) {
                    ZoomIndicator zoomIndicator = (ZoomIndicator) bean.bottomLayout;
                    zoomIndicator.setLoopData(bean);
                }
                if (bean.bottomLayout instanceof NormalIndicator){
                    NormalIndicator normalIndicator = (NormalIndicator) bean.bottomLayout;
                    normalIndicator.setViewPagerData(bean);
                }
                if (bean.bottomLayout instanceof TransIndicator){
                    TransIndicator transIndicator = (TransIndicator) bean.bottomLayout;
                    transIndicator.setViewPagerData(bean);
                }
            }
        }
    }

    class  CusTomAdapter extends PagerAdapter {
        @Override
        public int getCount() {
            return mViews.size();
        }
        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }
        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            //return super.instantiateItem(container, position);
            container.addView(mViews.get(position));
            return mViews.get(position);
        }
        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(mViews.get(position));
        }
    }



}